package com.errol.batchprocessor.payroll.advisorimpl.common;

import com.renjia.enums.*;
import com.renjia.service.module.employeeaddition.advisorimpl.payroll.payload.PayrollEmployeeBatchPayload;
import com.renjia.service.module.employeeaddition.advisorimpl.payroll.payload.PayrollEmployeePiecePayload;
import com.renjia.service.module.employeeaddition.advisorimpl.payroll.payload.PayrollEmployeeSinglePayload;
import com.renjia.service.module.payroll.advisor.upload.PayrollUploadCreateAdvisor;
import com.renjia.service.module.payroll.advisor.verify.PayrollVerifySingleFailedAdvisor;
import com.renjia.service.module.payroll.advisor.verify.PayrollVerifySingleSuccessAdvisor;
import com.renjia.service.module.payroll.advisorimpl.payload.PayrollUploadBatchPayload;
import com.renjia.service.module.payroll.advisorimpl.payload.PayrollUploadSinglePayload;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 发放各类状态参数配置
 *
 * @author zhang xiao bin
 * @version v1.0
 * @date 2021/03/17
 * @since v5.0.6
 **/
@Service
public class PayrollStatusAdvisorImpl implements
        PayrollUploadCreateAdvisor, PayrollVerifySingleSuccessAdvisor, PayrollVerifySingleFailedAdvisor {

    @Override
    public boolean shallProcess(@NonNull PayrollEmployeeBatchPayload advisorPayload) {
        return true;
    }

    @Override
    public int priority() {
        return 151;
    }

    @Override
    public boolean doAfterNewPayrollRecordAtUploadStage(@NonNull PayrollUploadBatchPayload batchPayload) {
        // 批次初始化状态
        batchPayload.getBatchEntity().setValidStatus(PayrollValidateStatusEnum.WAITING.getCode());
        batchPayload.getBatchEntity().setPayrollStatus(PayrollStatusEnum.SUBMIT.getCode());
        batchPayload.getBatchEntity().setCheckAcceptStatus(CheckAcceptStatusEnum.PUT_OUT.getCode());
        batchPayload.getBatchEntity().setCheckStatus(PayrollCheckStatusEnum.NOT_APPLY.getCode());
        return true;
    }

    @Override
    public boolean doAfterNewPayrollDetailAtUploadStage(@NonNull PayrollUploadSinglePayload singlePayload, @NonNull PayrollUploadBatchPayload batchPayload) {
        // 明细初始化状态
        singlePayload.getBusPayrollRecordDetail().setValidStatus(PayrollDetailValidateStatusEnum.VALIDATING.getCode());
        singlePayload.getBusPayrollRecordDetail().setPayrollStatus(PayrollDetailStatusEnum.PAY_WAIT.getCode());
        return true;
    }

    @Override
    public boolean doBeforeModifyPayrollDetailAtUploadStage(@NonNull PayrollUploadSinglePayload singlePayload, @NonNull PayrollUploadBatchPayload batchPayload) {
        // 编辑明细前不需要重置状态
        return true;
    }

    @Override
    public boolean doSettleOnPayrollDetailValidateSuccessAtVerifyStage(@NonNull PayrollEmployeeSinglePayload singlePayload, @NonNull PayrollEmployeePiecePayload piecePayload, @NonNull PayrollEmployeeBatchPayload batchPayload) {
        // 明细验证通过状态
        singlePayload.getSingleEntity().setValidStatus(PayrollDetailValidateStatusEnum.VALIDATE_PASS.getCode());
        singlePayload.getSingleEntity().setValidResult("验证通过");
        singlePayload.getSingleEntity().setRemark1("验证通过");
        singlePayload.getSingleEntity().setValidFinishTime(new Date());
        return true;
    }

    @Override
    public boolean doRecoverOnPayrollDetailValidateFailedAtVerifyStage(@NonNull PayrollEmployeeSinglePayload singlePayload, @NonNull PayrollEmployeePiecePayload piecePayload, @NonNull PayrollEmployeeBatchPayload batchPayload, @NonNull String errMsg) {
        // 明细验证失败状态
        singlePayload.getSingleEntity().setValidStatus(PayrollDetailValidateStatusEnum.VALIDATE_FAIL.getCode());
        singlePayload.getSingleEntity().setValidResult(errMsg);
        singlePayload.getSingleEntity().setRemark1(errMsg);
        singlePayload.getSingleEntity().setValidFinishTime(new Date());
        singlePayload.getSingleEntity().setPayrollStatus(PayrollDetailStatusEnum.UNDO.getCode());
        return true;
    }
}
